<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="./css/inputnumber.css" />
  <link rel="stylesheet" href="./css/index.css" />
  <title>购物车</title>
</head>

<body>
  <div class="app-container" id="app">
    <!-- 顶部banner -->
    <div class="banner-box"><img src="./img/fruit.jpg" alt="" /></div>
    <!-- 面包屑 -->
    <div class="breadcrumb">
      <span>🏠</span>
      /
      <span>购物车</span>
    </div>
    <!-- 购物车主体 -->
    <div class="main" v-show="fruits.length !== 0">
      <div class="table">
        <!-- 头部 -->
        <div class="thead">
          <div class="tr">
            <div class="th">选中</div>
            <div class="th th-pic">图片</div>
            <div class="th">单价</div>
            <div class="th num-th">个数</div>
            <div class="th">小计</div>
            <div class="th">操作</div>
          </div>
        </div>
        <!-- 身体 -->
        <div class="tbody">

          <div :class="index % 2 === 0 ? 'tr active':'tr'" v-for="(fruit,index) in fruits">
            <div class="td"><input type="checkbox" :checked="isChecked"/></div>
            <div class="td"><img :src="fruit.icon" alt="" /></div>
            <div class="td">{{fruit.price}}</div>
            <div class="td">
              <div class="my-input-number">
                <button class="decrease" :disabled="fruit.num <= 1" @click="decrease(index)"> - </button>
                <span class="my-input__inner">{{fruit.num}}</span>
                <button class="increase" @click="increase(index)"> + </button>
              </div>
            </div>
            <div class="td">{{fruit.price * fruit.num}}</div>
            <div class="td"><button @click="fruits.splice(index,1)">删除</button></div>
          </div>

        </div>
      </div>
      <!-- 底部 -->
      <div class="bottom">
        <!-- 全选 -->
        <label class="check-all">
          <input type="checkbox" @change="checkAll()" v-model="isChecked"/>
          全选
        </label>
        <div class="right-box">
          <!-- 所有商品总价 -->
          <span class="price-box">总价&nbsp;&nbsp;:&nbsp;&nbsp;¥&nbsp;<span class="price">{{totalPrice()}}</span></span>
          <!-- 结算按钮 -->
          <button class="pay">结算( {{totalNum()}} )</button>
        </div>
      </div>
    </div>
    <!-- 空车 -->
    <div class="empty" v-show="fruits.length === 0">🛒空空如也</div>
  </div>
  <script src="../js/vue.js"></script>
  <script>

    new Vue({
      el: "#app",
      data(){
        return {
            fruits: [
            {
              icon: './img/火龙果.png',
              num: 2,
              price: 6,
            },
            {
              icon: './img/荔枝.png',
              num: 7,
              price: 20,
            },
            {
              icon: './img/榴莲.png',
              num: 3,
              price: 40,
            },
            {
              icon: './img/鸭梨.png',
              num: 10,
              price: 3,
            },
            {
              icon: './img/樱桃.png',
              num: 20,
              price: 34,
            }
          ],
          isChecked : false
        }
      },
      methods: {
          totalPrice(){
              // 计算总价格
              let totalPrice = 0;
              // 把每个水果对象的小计累加，既是总价格
              for(let i = 0;i < this.fruits.length;i++){
                totalPrice += this.fruits[i].price * this.fruits[i].num;
              }
              return totalPrice;
          },
          totalNum(){
            // 计算总数量
            let totalNum = 0;
            // 把每个水果对象的数量累加，既是总数
            for(let i = 0;i < this.fruits.length;i++){
              totalNum += this.fruits[i].num;
            }
            return totalNum;
          },
          decrease(index){
            console.log("decrease",index);
            // 减水果的数量
            this.fruits[index].num--;
          },
          increase(index){
            console.log("increase",index);
            this.fruits[index].num++;
          },
          checkAll(){
            console.log(this.isChecked);
          }
      }
    });
  </script>
</body>

</html>